package com.poly.police.monitor.application.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.poly.police.monitor.application.cache.ConfigCache;
import com.poly.police.monitor.application.mapper.sys.SysConfigMapper;
import com.poly.police.monitor.common.util.MapUtils;
import com.poly.police.monitor.common.util.SpringUtils;
import com.poly.police.monitor.common.util.StringUtils;
import com.poly.police.monitor.pojo.entity.sys.SysConfig;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库配置操作工具
 */
public class ConfigUtils {

    /**
     * 根据类型获取配置
     * @author fzr
     * @param type 类型
     * @return Map<String, String>
     */
    public static Map<String, String> get(String type) {
        Map<String, String> cache = ConfigCache.get(type);
        if (!cache.isEmpty()) {
            return cache;
        }

        SysConfigMapper model = SpringUtils.getBean(SysConfigMapper.class);
        List<SysConfig> configs = model.selectList(
                new QueryWrapper<SysConfig>()
                        .select("id", "type"+"", "name", "value")
                        .eq("type", type));

        Map<String, String> map = new LinkedHashMap<>();
        for (SysConfig config : configs) {
            map.put(config.getName(), config.getValue());
        }

        ConfigCache.set();
        return map;
    }

    /**
     * 根据类型和名称获取配置
     *
     * @author fzr
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static String get(String type, String name) {
        String cache = ConfigCache.get(type, name);
        if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
            return cache;
        }

        SysConfigMapper model = SpringUtils.getBean(SysConfigMapper.class);
        SysConfig config = model.selectOne(
                new QueryWrapper<SysConfig>()
                        .select("id", "type", "name", "value")
                        .eq("name", name)
                        .eq("type", type));

        ConfigCache.set();
        return config.getValue();
    }

    /**
     * 根据类型和名称获取配置
     *
     * @author fzr
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static String get(String type, String name, String defaults) {
        String cache = ConfigCache.get(type, name);
        if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
            return cache;
        }

        SysConfigMapper model = SpringUtils.getBean(SysConfigMapper.class);
        SysConfig config = model.selectOne(
                new QueryWrapper<SysConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type)
                        .eq("name", name));

        if (config == null) {
            return defaults;
        }

        ConfigCache.set();
        return config.getValue();
    }

    /**
     * 根据类型和名称获取配置(JSON自定转Map)
     *
     * @author fzr
     * @param type 类型
     * @param name 名称
     * @return String
     */
    public static Map<String, String> getMap(String type, String name) {
        String cache = ConfigCache.get(type, name);
        if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
            return MapUtils.jsonToMap(cache);
        }

        SysConfigMapper model = SpringUtils.getBean(SysConfigMapper.class);

        SysConfig config = model.selectOne(
                new QueryWrapper<SysConfig>()
                        .select("id", "type", "name", "value")
                        .eq("type", type)
                        .eq("name", name));

        if (config == null) {
            return null;
        }

        if (config.getValue().equals("") || config.getValue().equals("[]") || config.getValue().equals("{}")) {
            return new LinkedHashMap<>();
        }

        ConfigCache.set();
        return MapUtils.jsonToMap(config.getValue());
    }

    /**
     * 设置配置的值
     *
     * @author fzr
     * @param type 类型
     * @param name 名称
     * @param val 值
     */
    public static void set(String type, String name, String val) {
        SysConfigMapper model = SpringUtils.getBean(SysConfigMapper.class);
        SysConfig config = model.selectOne(
                new QueryWrapper<SysConfig>()
                        .eq("type", type)
                        .eq("name", name));

        if (config != null) {
            config.setValue(val);
            config.setUpdateTime(new Date());
            model.updateById(config);
        } else {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setType(type);
            sysConfig.setName(name);
            sysConfig.setValue(val);
            sysConfig.setCreateTime(new Date());
            sysConfig.setUpdateTime(new Date());
            model.insert(sysConfig);
        }

        ConfigCache.set();
    }

}
